package com.tmy.service.Impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tmy.Dto.CommonDto;
import com.tmy.Dto.Result;
import com.tmy.entity.Hotel;
import com.tmy.entity.RoomInfo;
import com.tmy.mapper.HotelMapper;
import com.tmy.mapper.RoomInfoMapper;
import com.tmy.service.HotelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员业务处理
 *
 * @author 谭明玥
 */
@Service
public class HotelServiceImpl implements HotelService {

    @Resource
    private HotelMapper hotelMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;


    /**
     * 分页查询
     */
    @Override
    public Result queryHotelPage(com.tmy.Dto.PageInfo pageInfo){
        Result result =new Result();
        try {
            Page<Hotel> page = new Page<>(pageInfo.getPage(),pageInfo.getLimit());
            Hotel hotel = JSON.parseObject(JSON.toJSONString(pageInfo.getParam()),Hotel.class);
            LambdaQueryWrapper<Hotel> lqw = new LambdaQueryWrapper<>();
            if (hotel.getName()!=null){
                lqw.like(Hotel::getName,hotel.getName());
            }
            if (hotel.getAddress()!=null){
                lqw.like(Hotel::getAddress,hotel.getAddress());
            }
            if (hotel.getLocation()!=null){
                lqw.like(Hotel::getLocation,hotel.getLocation());
            }
            if (hotel.getPrice()>1){
                lqw.like(Hotel::getPrice,hotel.getPrice());
            }
            Page<Hotel> hotelPage = hotelMapper.selectPage(page,lqw );
            result.setCode(200);
            result.setMsg("酒店分页查询成功");
            result.setData(hotelPage);
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     * 查询酒店
     */
    @Override
    public Result queryHotel(Hotel hotel){
        Result result =new Result();
        try {
            LambdaQueryWrapper<Hotel> lqwHotel = new LambdaQueryWrapper<>();
            lqwHotel.eq(Hotel::getId,hotel.getId());
            List<Hotel> hotelList= hotelMapper.selectList(lqwHotel);
            LambdaQueryWrapper<RoomInfo> lqw = new LambdaQueryWrapper<>();
            lqw.eq(RoomInfo::getHotelId,hotel.getId());
            List<RoomInfo> roomInfos = roomInfoMapper.selectList(lqw);
            hotelList.get(0).setRoomList(roomInfos);
            result.setCode(200);
            result.setMsg("酒店信息查询成功");
            result.setData(hotelList.get(0));
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }




    /**
     * 新增接口
     */
    @Override
    public Result addHotel(Hotel hotel){
        Result result =new Result();
        try {
            hotelMapper.insert(hotel);
            result.setCode(200);
            result.setMsg("酒店新增成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     * 删除接口
     */
    @Override
    public Result deleteHotel(Hotel hotel){
        Result result =new Result();
        try {
            hotelMapper.deleteById(hotel);
            result.setCode(200);
            result.setMsg("酒店删除成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 修改接口
     */
    @Override
    public Result updateHotel(Hotel hotel){
        Result result =new Result();
        try {
            hotelMapper.updateById(hotel);
            result.setCode(200);
            result.setMsg("信息修改成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }

    /**
     * 批量删除接口
     */
    @Override
    public Result deletesHotel(CommonDto commonDto){
        Result result =new Result();
        try {
            List<Integer> ids =commonDto.getDeleteIds();
            hotelMapper.deleteBatchIds(ids);
            result.setCode(200);
            result.setMsg("酒店删除成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
}